/**    
 * @{#} BuweiActivity.java Create on 2014-6-9 下午8:50:36    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import cn.o.app.event.listener.OnSelectedChangeListener;
import cn.o.app.json.JsonUtil;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.ui.OTab;
import cn.o.app.ui.OTabItem;
import cn.skinapp.R;

import com.evan.common.pullrefresh.LoadingLayout;
import com.evan.common.pullrefresh.PullToRefreshBase;
import com.evan.common.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.evan.common.pullrefresh.PullToRefreshListView;
import com.smiier.skin.adapter.QuestionAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.QuestionGetListTask;
import com.smiier.skin.net.QuestionGetListTask.QuestionGetListRequest;
import com.smiier.skin.net.QuestionGetListTask.QuestionGetListResponse;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class MyQuestionDetailActivity extends BasicActivity implements
		OnRefreshListener<ListView>, OnItemClickListener {

	private PullToRefreshListView list_following_question;
	private TextView text_empty;
	private QuestionAdapter questionAdapter;
	Long start_qid;
	ArrayList<Object> array = new ArrayList<Object>();

	private boolean mShowSolving = true;
	private boolean mShowSolved = true;
	private LoadingLayout mLoading;
	private OTab mTab;
	private OTabItem mSolving;
	private OTabItem mSolved;
	private ArrayList<Integer> status12 = new ArrayList<Integer>();
	private ArrayList<Integer> status3 = new ArrayList<Integer>();
	int Is_Appraise;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_question_detail);
		init();
		setNavTitle("我的咨询");
		mTab = findViewById(R.id.tab, OTab.class);
		mSolving = findViewById(R.id.solving, OTabItem.class);
		mSolved = findViewById(R.id.solved, OTabItem.class);
		mTab.setOnSelectedChangeListener(new OnSelectedChangeListener() {

			@Override
			public void onChanged(View v, int index) {
				start_qid = null;
				array.clear();
				if (index == 0) {
					Is_Appraise = 0;
					status = status12;
				} else if (index == 1) {
					status = status3;
					Is_Appraise = 1;
				}
				loadData(false);
			}
		});
		status12.add(1);
		status12.add(2);
		status12.add(3);
		status3.add(3);
		list_following_question = (PullToRefreshListView) findViewById(R.id.list_question_detail);
		list_following_question.setOnRefreshListener(this);
		list_following_question.setPullRefreshEnabled(false);
		list_following_question.setScrollLoadEnabled(true);
		mLoading = list_following_question.getFooterLoadingLayout();
		text_empty = (TextView) findViewById(R.id.text_empty);
		text_empty.setText("尚无咨询的问题");

		list_following_question.doPullRefreshing(true);
		list_following_question.setOnItemClickListener(this);

		status = status3;
		loadData(false);
		status = status12;
		loadData(false);

	}

	@Override
	protected void onResume() {
		super.onResume();
//		MobclickAgent.onResume(this);
		start_qid = null;
		array.clear();
		loadData(false);
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		start_qid = null;
		array.clear();
		loadData(false);
	}

	@Override
	public void onPullUpToLoadMore(PullToRefreshBase<ListView> refreshView) {

		loadData(false);
	}

	private Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			int type = msg.what;
			if (type == 1) {
				array.clear();
				mShowSolved = false;
				mSolving.setText("解答中" + " (" + msg.arg1 + ")");
			} else if (type == 3) {
				mShowSolving = false;
				mSolved.setText("已完成" + " (" + msg.arg1 + ")");
			} else if (type == 100) {
				text_empty.setVisibility(View.VISIBLE);
				mLoading.setVisibility(View.INVISIBLE);
			} else if (type == 101) {
				text_empty.setVisibility(View.GONE);
				mLoading.setVisibility(View.VISIBLE);

			}
		};
	};
	ArrayList<Integer> status = new ArrayList<Integer>();

	public void loadData(boolean isShowDialog) {

		final QuestionGetListRequest req = new QuestionGetListRequest();
		req.pageSize = Constant.pageSize;
		req.startQid = start_qid;
		req.token = GlobalSettings.sToken;
		req.statuses = status;
		req.uid = GlobalSettings.sUid;
		if (req.statuses.size() > 1) {
			req.Is_Appraise = 0;
		} else {
			req.Is_Appraise = 1;
		}

		QuestionGetListTask task = new QuestionGetListTask();
		task.setRequest(req);
		task.addListener(new NetTaskListener<QuestionGetListTask.QuestionGetListRequest, QuestionGetListTask.QuestionGetListResponse>() {

			@Override
			public void onComplete(
					INetTask<QuestionGetListRequest, QuestionGetListResponse> task,
					QuestionGetListResponse response) {
				if (response.ResultCode != 200) {
					return;
				}
				if (!CommonUtility.isNull(response)) {
					h.sendEmptyMessage(101);
					try {
						JSONArray user_array = JsonUtil
								.convertToArray(response.Res.Questions);
						if (user_array.length() > 0) {
							start_qid = user_array.getJSONObject(
									user_array.length() - 1).getLong("Qid");
						}

						list_following_question.doComplete();
						try {
							if (mShowSolved || mShowSolving) {
								Message msg = new Message();
								msg.what = req.statuses.get(0);
								msg.arg1 = response.Res.Count;
								h.sendMessage(msg);
								return;
							}
							if (user_array.length() > 0) {
								
//								CommonUtility.putAll(array, user_array, true);
								
								CommonUtility.putAllAlong(array,user_array);
								
								if (CommonUtility.isNull(questionAdapter)) {
									questionAdapter = new QuestionAdapter(
											activity, array);
									list_following_question
											.setAdapter(questionAdapter);
								} else {
									questionAdapter.setList(array);
									questionAdapter.notifyUpdate();
								}
								if (user_array.length() < Constant.pageSize) {
									list_following_question
											.setHasMoreData(false);
								} else {
									list_following_question
											.setHasMoreData(true);
								}
							} else {

								list_following_question.setHasMoreData(false);
								questionAdapter.notifyUpdate();
							}

						} catch (Exception e) {
							e.printStackTrace();
						}

						if (array.size() < 1) {
							h.sendEmptyMessage(100);
						} else {
							list_following_question.setHasMoreData(false);
						}

					} catch (Exception e) {
						e.printStackTrace();
					}
				}

			}

			@Override
			public void onException(
					INetTask<QuestionGetListRequest, QuestionGetListResponse> task,
					Exception e) {

			}
		});
		add(task);
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int arg2,
			long arg3) {
		JSONObject object = (JSONObject) view.getTag(R.id.tag_obj);
		Intent intent = new Intent(activity, QuestionDetailActivity.class);
		intent.putExtra(Constant.IDENTITY_KEY, object.toString());
		startActivity(intent);
	}

/*	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
